package com.erebus.view.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erebus.base.model.PageResult;
import com.erebus.view.mapper.ClassesMapper;
import com.erebus.view.mapper.StudentMapper;
import com.erebus.view.mapper.TeacherClassMapper;
import com.erebus.view.model.dto.ListParams;
import com.erebus.view.model.dto.StudentDto;
import com.erebus.view.model.po.Classes;
import com.erebus.view.model.po.Student;
import com.erebus.view.model.po.TeacherClass;
import com.erebus.view.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class StudentServiceImpl implements StudentService {


    @Autowired
    private TeacherClassMapper teacherClassMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassesMapper classesMapper;


    // 查询所有
    @Override
    public PageResult<StudentDto> list(ListParams listParams) {
        //构建查询条件对象
        LambdaQueryWrapper<TeacherClass> queryWrapper = new LambdaQueryWrapper<>();

        // 根据教师id，查出所教班级id
        queryWrapper.eq(StringUtils.isNotEmpty(listParams.getTeacherId()), TeacherClass::getTeacherId, listParams.getTeacherId());
        List<TeacherClass> classList = teacherClassMapper.selectList(queryWrapper);

        // 根据所教班级id，查出所有学生

        //构建查询条件对象
        LambdaQueryWrapper<Student> queryWrapper2 = new LambdaQueryWrapper<>();

        //构建查询条件，根据名查询
        queryWrapper2.like(StringUtils.isNotEmpty(listParams.getName()), Student::getName, listParams.getName());
        //构建查询条件，根据学号查询
        queryWrapper2.eq(StringUtils.isNotEmpty(listParams.getStudentId()), Student::getStudentId, listParams.getStudentId());
        //构建查询条件，根据性别查询
        queryWrapper2.eq(listParams.getSex() != null, Student::getSex, listParams.getSex());

        //分页对象
        Page<Student> page = new Page<>(listParams.getPageNo(), listParams.getPageSize());
        // 查询数据内容获得结果
        Page<Student> pageResult = studentMapper.selectPage(page, queryWrapper2);
        // 获取数据列表
        List<Student> list = pageResult.getRecords();

        ArrayList<StudentDto> studentDtoList = new ArrayList<>();
        // 删除不在这些班级的学生
        for (Student student : list) {
            for (TeacherClass teacherClass : classList) {
                if (teacherClass.getClassId().equals(student.getClassId())) {
                    StudentDto studentDto = new StudentDto();
                    // 执行属性赋值操作，将 Student 对象的属性值复制到 StudentDto 对象
                    BeanUtils.copyProperties(student, studentDto);
                    // 根据classId查询所在班级
                    studentDto.setClassName(ClassNameById(studentDto.getClassId()));
                    studentDtoList.add(studentDto);
                    break;
                }
            }
        }

        // 获取数据总数
        long total = pageResult.getTotal();
        // 构建结果集
        PageResult<StudentDto> studentPageResult = new PageResult<>(studentDtoList, total, listParams.getPageNo(), listParams.getPageSize());


        return studentPageResult;
    }

    @Override
    public int add(StudentDto studentDto) {

        Student student = new Student();
        BeanUtils.copyProperties(studentDto, student);

        int total = 0;
        if (student != null) {
            total = studentMapper.insert(student);
        }

        return total;
    }

    @Override
    public StudentDto selectById(String studentId) {

        Student student = studentMapper.selectByStudentId(studentId);
        StudentDto studentDto = new StudentDto();

        BeanUtils.copyProperties(student, studentDto);

        return studentDto;
    }

    @Override
    public int update(StudentDto studentDto, String studentOldId) {


        Student student = new Student();
        BeanUtils.copyProperties(studentDto, student);

        //构建查询条件对象
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        //构建查询条件，根据名查询
        queryWrapper.eq(StringUtils.isNotEmpty(studentDto.getStudentId()), Student::getStudentId, studentOldId);

        int i = studentMapper.update(student, queryWrapper);


        return i;
    }

    @Override
    public int deleteById(String studentId) {

        //构建查询条件对象
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        //构建查询条件，根据名查询
        queryWrapper.eq(StringUtils.isNotEmpty(studentId), Student::getStudentId, studentId);

        int i = studentMapper.delete(queryWrapper);

        return i;
    }

    // 根据班级id 查询班名
    private String ClassNameById(String classId){
        Classes classes = classesMapper.byIdName(classId);

        return classes.getName();
    }
}
